package com.example.yuejian21.nongfusanquan.yuejian21.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.example.yuejian21.nongfusanquan.yuejian21.R;
import com.example.yuejian21.nongfusanquan.yuejian21.adapter.UserpostAdapter;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Userpostbean;
import com.example.yuejian21.nongfusanquan.yuejian21.model.Postmodel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Nongfusanquan on 2017-11-13.
 */

public class MypostActivity extends AppCompatActivity {

    private Button back_collect;
    private RecyclerView recyclerView;
    private int u_id;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mypost);
        initViews();
        initEvent();
        getData();
    }
    private void initViews(){
        back_collect = (Button)findViewById(R.id.return_mypost);
        recyclerView=(RecyclerView)findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局
    }

    private void initEvent(){
        back_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getData() {
        SharedPreferences sp =this.getSharedPreferences("cache", Context.MODE_APPEND);
        u_id = Integer.parseInt(sp.getString("uid","0"));

        Postmodel Model=new Postmodel();
        Call<List<Userpostbean>> call=Model.getMypost(u_id);
        call.enqueue(new Callback<List<Userpostbean>>() {
            @Override
            public void onResponse(Call<List<Userpostbean>> call, Response<List<Userpostbean>> response) {
                List<Userpostbean> data = response.body();  //获取数据
                recyclerView.setAdapter(new UserpostAdapter(MypostActivity.this, data));
            }

            @Override
            public void onFailure(Call<List<Userpostbean>> call, Throwable t) {

            }
        });

    }
}
